﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CF.Data.Entity.FrmMemberEntity;

namespace CF.Member.DAL.Badges
{
    /// <summary>
    /// 如虎添翼徽章
    /// </summary>
    public class TigerSpeedBadge : BaseBadge
    {
        public TigerSpeedBadge() : base(6, 0, 1, 10) { }

        public override bool CheckTakeCondition(int uid, int bid)
        {
            bool result = false;
            if (base.CheckTakeCondition(uid, bid))
            {
                using (caifendbEntities context = new caifendbEntities())
                {
                    FM_BadgeConfig bdcfg = context.FM_BadgeConfig.FirstOrDefault(c => c.BID == bid);
                    if (bdcfg != null)
                    {
                        int cclCount;
                        if (!int.TryParse(bdcfg.CheckParam, out cclCount))
                        {
                            cclCount = 7;
                        }
                        //获取连续登录次数
                        FM_Member mb = context.FM_Member.FirstOrDefault(c => c.UID == uid);
                        if (mb != null && mb.ConcatLoginCount >= cclCount)
                        {
                            result = true;
                        }
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// 升级公式：4*(m2-1)，m是徽章等级，1 <= m <= 10；
        /// 登录1次加1点徽章经验值，每天只加一次经验值，达到临界点的时候自动升级。
        /// </summary>
        /// <returns></returns>
        public override UpdateBadgeStatus UpdateBadgeLevel(int bid, int uid)
        {
            UpdateBadgeStatus result = UpdateBadgeStatus.Default;
            using (caifendbEntities context = new caifendbEntities())
            {
                FM_MyBadges myBadge = context.FM_MyBadges.FirstOrDefault(c => c.BID == bid && c.UID == uid && c.Status == BadgeActState.Activation);
                if (myBadge != null)
                {
                    DateTime updTime = myBadge.LastUpdateTime ?? DateTime.Now;
                    int daySpan = CommonCls.DateUtils.DayDifference(updTime, DateTime.Now);
                    //判断是否当天是否已经加过经验值
                    if (daySpan >= 1)
                    {
                        int param = myBadge.CurrParam ?? 0;
                        param++;
                        myBadge.CurrParam = param;
                        myBadge.LastUpdateTime = DateTime.Now;
                        byte blvl = myBadge.BadgeLevel ?? BaseBadgeLevel;
                        result = UpdateBadgeStatus.AddGrowUpPoint;
                        //判断是否能升级
                        if (blvl < MaxBadgeLevel)
                        {
                            int nextLvl = blvl + 1;
                            int updLvl = 4 * (nextLvl * nextLvl - 1);
                            if (myBadge.CurrParam >= updLvl)
                            {
                                blvl++;
                                myBadge.BadgeLevel = (byte)blvl;
                                int exp = myBadge.CurrAwardExp ?? BaseAwardExp;
                                exp++;
                                myBadge.CurrAwardExp = exp;
                                result = UpdateBadgeStatus.UpdateSuccess;
                            }
                        }
                        else
                        {
                            result = UpdateBadgeStatus.FullLevel;
                        }
                        context.SaveChanges();
                    }
                    else
                    {
                        result = UpdateBadgeStatus.UpdateFailed;
                    }
                }
                else
                {
                    result = UpdateBadgeStatus.NotFound;
                }
            }
            return result;
        }
    }
}
